<?php
/**
 * lemocms
 * ============================================================================
 * 版权所有 2018-2027 lemocms，并保留所有权利。
 * 网站地址: https://www.lemocms.com
 * ----------------------------------------------------------------------------
 * 采用最新Thinkphp6实现
 * ============================================================================
 * Author: yuege
 * Date: 2019/8/26
 */
namespace app\admin\controller;
use app\common\model\ArticleCate;
use think\facade\Db;
use think\facade\Request;
use think\facade\View;



class Article extends  Base{

    public function initialize()
    {
        parent::initialize(); // TODO: Change the autogenerated stub
    }
    
    /**
     * 获取token值
     * @return type
     */
    public static function getToken(){
        $url = "https://www.shiankuaixian.com/api.php";
        $data = array(
            "_app_name"=>"com.SAKX.FoodSafetyAdmin",
            "_secret"=>"WZvpokjLLRTO44gj",
            "g"=>"Api2.0",
            "m"=>"Public",
            "c"=>"postToken"
        );
        $output = curl_post($url, $data);
        return $output;
          
    }

    public function index(){
        if(Request::isGet()){
            $keys = Request::post('keys','','trim');
            $page = Request::post('page') ? Request::post('page') : 1;
            $limit = Request::post('limit') ? Request::post('limit') : 1;
            // $list = Db::name('article')->alias('a')
            //     ->join('article_cate ac','a.pid = ac.id','left')
            //     ->field('a.*,ac.title as cate_name')
            //     ->where('a.title|a.content','like','%'.$keys.'%')
            //     ->order('a.sort desc,a.id desc')
            //     ->paginate(['list_rows' => $this->pageSize, 'page' => $page])
            //     ->toArray();
            // return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total']];

        }
       
        View::assign([
            'page' => $page,
            'limit' => $limit
        ]);

        return View::fetch();

    }

    public function add(){
        if(Request::isPost()) {
            $data = Request::post();
            $res = \app\common\model\Article::create($data);
            if ($res) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');

            }
        }else{

            $ArticleCate = ArticleCate::where('status',1)->select()->toArray();
            $ArticleCate= cate_tree($ArticleCate);
            $params['name'] = 'container';
            $params['content'] = '';
            $view = [
                'info' => '',
                'ArticleCate' => $ArticleCate,
                'title' => '添加',
                'ueditor'=>build_ueditor($params),
            ];
            View::assign($view);
            return View::fetch('add');
        }
    }

    public function edit()
    {
        // $api = new Api();
        //     $result = $api->getToken();
        //     dump($api);
        //     dump($result);
        // if(Request::isPost()){
        //     $data = Request::post();
        //     if(!$data['id']){
        //         $this->error('非法数据');
        //     }

        //     $res = \app\common\model\Article::update($data);
        //     if($res){
        //         $this->success('修改成功');
        //     }else{
        //         $this->error('修改失败');

        //     }
        // }else{
        //     $id =  Request::get('id');
        //     // $ArticleCate = ArticleCate::where('status',1)->select()->toArray();
        //     // $ArticleCate= cate_tree($ArticleCate);
            

        //     $info = \app\common\model\Article::find($id);
        //     $params['name'] = 'container';
        //     $params['content'] = $info['content'];
        //     $view = [
        //         'info' => $info,
        //         // 'ArticleCate' => $ArticleCate,
        //         'title' => '编辑',
        //         'ueditor'=>build_ueditor($params),
        //     ];
        //     View::assign($view);
        //     return View::fetch('add');
        // }
        $id =  Request::get('id');
        $returnData = Article::getToken();
        $return = (json_decode($returnData,true));
        $url = "https://www.shiankuaixian.com/api.php";
        $data = array(
            "_app_name"=>"com.SAKX.FoodSafetyAdmin",
            "g"=>"Api2.0",
            "m"=>"Content",
            "c"=>"public_getContentInfo",
            "_token" => $return['content']['access_token'],
            "id"=> $id,   
        );
        $output = curl_post($url, $data);
        $output = json_decode($output, true);
        $params['name'] = 'container';
        $params['content'] = $output['content']['content']['content'];
        $view = [
            'info' => $output['content']['content'],
            'title' => '编辑',
            'ArticleCate' => '',
            'ueditor' => build_ueditor($params),
        ];
        View::assign($view);
        return View::fetch('add');
       

    }

    public function state(){
        $data =  Request::post();
        $id = Request::post('id');
        if (empty($id)) {
            $this->error('数据不存在!');
        }
        $info  = \app\common\model\Article::find($id);
        $status = $info['status'] == 1 ? 0 : 1;
        $info->status = $status;
        $info->save();
        $this->success('修改成功!');

    }
    public function delete(){

        if(Request::isPost()){

            $id = Request::post('id');
            \app\common\model\Article::destroy($id);
            $this->success('删除成功');
        }

    }

    public function articleCate(){
        if(Request::isPost()){
            $keys = Request::post('keys','','trim');
            $page = Request::post('page') ? Request::post('page') : 1;
            $list= cache('articleCate');
            if(!$list) {
                $list = Db::name('article_cate')
                    ->where('title','like','%'.$keys.'%')
                    ->paginate(['list_rows' => $this->pageSize, 'page' => $page])
                    ->toArray();
                foreach($list['data'] as $k=>$v){
                    $list['data'][$k]['lay_is_open']=false;
                }
                cache('articleCate', $list, 3600);
            }

            return $result = ['code'=>0,'msg'=>'获取成功!','data'=>$list['data'],'count'=>$list['total']];

        }
        return View::fetch();
    }

    public function cateAdd(){
        if(Request::isPost()) {
            $data = Request::post();
            $res = \app\common\model\ArticleCate::create($data);
            if ($res) {
                $this->success('添加成功');
            } else {
                $this->error('添加失败');

            }
        }else{

            $ArticleCate = ArticleCate::where('status',1)->select()->toArray();
            $ArticleCate= cate_tree($ArticleCate);

            $view = [
                'info' => '',
                'ArticleCate' => $ArticleCate,
                'title' => '添加',
            ];
            View::assign($view);
            return View::fetch('cate_add');
        }

    }
    public function cateEdit(){
        if(Request::isPost()){
            $data = Request::post();
            if(!$data['id']){
                $this->error('非法数据');
            }

            $res = \app\common\model\ArticleCate::update($data);
            if($res){
                $this->success('修改成功');
            }else{
                $this->error('修改失败');

            }
        }else{
            $id =  Request::get('id');
            $ArticleCate = ArticleCate::where('status',1)->select()->toArray();
            $ArticleCate= cate_tree($ArticleCate);

            $info = \app\common\model\ArticleCate::find($id);

            $view = [
                'info' => $info,
                'ArticleCate' => $ArticleCate,
                'title' => '编辑',
            ];
            View::assign($view);
            return View::fetch('cate_add');
        }


    }

    public function cateState(){
        $data =  Request::post();
        $id = Request::post('id');
        if (empty($id)) {
            $this->error('数据不存在!');
        }
        $info  = \app\common\model\ArticleCate::find($id);
        $status = $info['status'] == 1 ? 0 : 1;
        $info->status = $status;
        $info->save();
        $this->success('修改成功!');

    }
    public function cateDel(){

        if(Request::isPost()){

            $id = Request::post('id');
            $child = \app\common\model\ArticleCate::where('pid',$id)->find();
            if($child){
                $this->error('请先删除下级');
            }
            \app\common\model\Article::destroy($id);
            $this->success('删除成功');
        }

    }


}